< previous page page_581 next page >

Page 581
Setting the Current Index in a Table
You can use an index with a table to establish a specific order for the records or to work with the Seek method to find specific records quickly. For an index to be in effect, the Index property of the table must be set to the name of an existing index for the table. An example of how to use a program command to set the current index follows:
rsTest.Index = "NameIndex"
The index specified for the table must be one that has already been created and is part of the indexes collection for the given table. If the index does not exist, an error occurs. The index is not created for you. To learn about creating a new index, see the section ''Creating a New Index" later in this chapter.
note.gif An index does not change the actual order of records in a table; it simply changes the order in which they are retrieved from the table and the order in which the table's records are searched. square.gif

Using the Seek Method
Using the Seek method is the fastest way to locate an individual record in a table; however, it is also the most limiting of the record-positioning methods. The following list outlines the limitations of the Seek method:
This method can be performed only on a table; you can't use it with a dynaset or snapshot.
This method can be used only with an active index; the parameters of the Seek method must match the fields of the index in use.
This method finds only the first record that matches the specified index values; subsequent uses do not find additional matching records.
How the Seek Method Works
The Seek method consists of the method call, the comparison operator, and the values of the key fields. The comparison operator can be <, <=, =, >=, >, or <>. The key values being compared must be of the same data type as the fields in the controlling index. Although you are not required to include the same number of key values as there are fields in the index, you do have to include a key value for each field you want to search. These values must appear in the same order as the fields in the index and be separated by commas. Listing 26.2 illustrates the use of the Seek method twice. The first part of the listing shows how to use Seek with an index based on one field; the second part uses an index based on two fields.
Listing 26.2 SEEKTEST.TXTUsing the Seek Method to Find a Specific Record in a Table
    Dim dbTest As Database, rsDealers As Recordset

    Set dbTest = OpenDatabase("C:\MARKET\MARKET.MDB")
    Set rsDealers = dbTest.OpenRecordset("Dealers", dbOpenTable)
continues

 
< previous page page_581 next page >